package com.aurora.oa.mycenter.view;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.R;
import com.aurora.oa.application.MyApplication;
import com.aurora.oa.customview.InputDialog;
import com.aurora.oa.mycenter.bean.UserCenterBean;
import com.aurora.oa.mycenter.bean.UserCenterData;
import com.aurora.oa.mycenter.presenter.MyCenterFragmentPresenter;
import com.aurora.oa.mycenter.presenter.MyCenterFragmentPresenterImpl;
import com.aurora.oa.utils.StringUtils;
import com.aurora.oa.utils.ToastUtil;
import com.facebook.drawee.view.SimpleDraweeView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by Administrator on 2017/3/14 0014.
 */
public class MyCenterFragment extends Fragment implements MyCenterFragmentView {

    @Bind(R.id.tv_depart)
    TextView tvDepart;
    @Bind(R.id.tv_creat_team)
    TextView tvCreatTeam;
    @Bind(R.id.tv_my_team)
    TextView tvMyTeam;
    @Bind(R.id.tv_creat_contacts)
    TextView tvCreatContacts;
    @Bind(R.id.tv_my_contacts)
    TextView tvMyContacts;
    @Bind(R.id.tv_change_info)
    TextView tvChangeInfo;
    @Bind(R.id.tv_setting)
    TextView tvSetting;
    private SimpleDraweeView sdv_headImg;
    private TextView tv_realName;
    private TextView tv_sign;
    private TextView tv_overtime;
    private TextView tv_leave;
    private TextView tv_tiaoxiu;
    private TextView tv_late;
    private MyCenterFragmentPresenter myCenterFragmentPresenter = new MyCenterFragmentPresenterImpl(this);
    //存储的参
    private String mParam;

    //获取 fragment 依赖的 Activity，方便使用 Context
    private Activity mAct;
    private ProgressDialog progressDialog;

/*    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    showUserCurrentMonthData((UserCenterData) msg.obj);
                    break;
                default:
                    break;
            }
        }
    };*/
    private InputDialog dialog;

    @Override
    public void showUserCurrentMonthData(UserCenterData mData) {
        UserCenterBean useCenterBean = mData.getObj();
        sdv_headImg.setImageURI(AuroraConstants.BASE_SERVER_URL + "/" + useCenterBean.getHeadImg());
        tv_realName.setText(useCenterBean.getUserRealName());
        tv_sign.setText(useCenterBean.getSignNum() + "");
        tv_overtime.setText(useCenterBean.getOvertimeNum() + "");
        tv_leave.setText(useCenterBean.getLeavesNum() + "");
        tv_tiaoxiu.setText(useCenterBean.getTiaoxiuNum() + "");
        tv_late.setText(useCenterBean.getLateNum() + "");
    }

    @Override
    public void showMessage(String message) {
        ToastUtil.show(mAct,message);
    }

    @Override
    public void showProgressDialog(String message) {
        progressDialog = ProgressDialog.show(mAct,null,message);
    }

    @Override
    public void disMissProgressDialog() {
        progressDialog.dismiss();
    }

    public static MyCenterFragment newInstance(String param) {
        MyCenterFragment fragment = new MyCenterFragment();
        Bundle args = new Bundle();
        args.putString("param", param);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            mParam = savedInstanceState.getString("param");
        }
        mAct = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_mycenter_main, container, false);
        ButterKnife.bind(this,view);
        String depart =MyApplication.getInstance().getLoginUser().getDepartment();
        if(StringUtils.isNullString(depart)){
            tvDepart.setText("暂无部门");
        }else{
            tvDepart.setText(depart);
        }
        sdv_headImg = (SimpleDraweeView) view.findViewById(R.id.sdv_head_img);
        tv_realName = (TextView) view.findViewById(R.id.tv_real_name);
        tv_sign = (TextView) view.findViewById(R.id.tv_sign_num);
        tv_overtime = (TextView) view.findViewById(R.id.tv_over_time);
        tv_leave = (TextView) view.findViewById(R.id.tv_leave_num);
        tv_tiaoxiu = (TextView) view.findViewById(R.id.tv_tiaoxiu);
        tv_late = (TextView) view.findViewById(R.id.tv_late);

     //   myCenterFragmentPresenter = new MyCenterFragmentPresenterImpl(mHandler);
      //  ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        myCenterFragmentPresenter.getMyCenterDataHttp();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    @OnClick({R.id.tv_setting,R.id.tv_creat_team, R.id.tv_my_team, R.id.tv_creat_contacts, R.id.tv_my_contacts, R.id.tv_change_info})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_creat_team:
                final InputDialog.Builder teamBuilder = new
                        InputDialog.Builder(mAct);
                teamBuilder.setTitle("组建团队")
                        .setMessage("请输入团队名称")
                        .setNegativeButton("取消",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                })
                        .setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                        myCenterFragmentPresenter.creatTeam(teamBuilder.getInput());
                                    }
                                });
                dialog = teamBuilder.create();
                dialog.show();
                break;
            case R.id.tv_my_team:
                mAct.startActivity(new Intent(mAct,MyTeamListActivity.class));
                break;
            case R.id.tv_creat_contacts:
                final InputDialog.Builder contactsBuilder = new
                        InputDialog.Builder(mAct);
                contactsBuilder.setTitle("组建通讯录")
                        .setMessage("请输入通讯录名称")
                        .setNegativeButton("取消",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                })
                        .setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                        myCenterFragmentPresenter.creatContacts(contactsBuilder.getInput());
                                    }
                                });
                dialog = contactsBuilder.create();
                dialog.show();
                break;
            case R.id.tv_my_contacts:
                mAct.startActivity(new Intent(mAct,MyContactsGroupListActivity.class));
                break;
            case R.id.tv_change_info:
                mAct.startActivity(new Intent(mAct,MyInfoEditActivity.class));
                break;
            case R.id.tv_setting:
                mAct.startActivity(new Intent(mAct,SettingActivity.class));
                break;
        }
    }
}
